﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coliseu.BussinessLayer;

namespace coliseu.Views.Rotinas_Diarias
{
    public partial class FrmListVisitante : Form
    {
        private VisitanteBL vBL = new VisitanteBL();
        private Visitante_ApartamentoBL vaBL = new Visitante_ApartamentoBL();

        public FrmListVisitante()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {


        }

        private void btnaddapto_Click(object sender, EventArgs e)
        {
            FrmCadVisitanteVisita x = new FrmCadVisitanteVisita(((visitante)visitanteBindingSource.Current).id_visitante, ((visitante)visitanteBindingSource.Current).nome,vaBL);
            x.ShowDialog();

            carregaGrid();
        }

        private void btncancelapto_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = dtGrdVwBloco.CurrentRow;
                int x = Convert.ToInt32(row.Cells["id_visitante_apartamento"].Value);

                vaBL.excluir(x);

                carregaGrid();
            }
            catch
            {
            }
        }

        private void FrmListVisitante_Load(object sender, EventArgs e)
        {
            //  visitanteBindingSource.DataSource = vBL.selecionar();

            //            if (visitanteBindingSource.Count > 0)
            //              fotoPictureBox.Image = Classes.Utils.byteArrayToImage(((visitante)visitanteBindingSource.Current).foto);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            FrmCadVisitante c = new FrmCadVisitante(vBL, ((visitante)visitanteBindingSource.Current));
            c.ShowDialog();
            visitanteBindingSource.DataSource = vBL.selecionar();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FrmCadVisitante c = new FrmCadVisitante(vBL);
            c.ShowDialog();
            visitanteBindingSource.DataSource = vBL.selecionar();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) || !string.IsNullOrEmpty(textBox2.Text))
            {

                visitanteBindingSource.DataSource = vBL.selecionar(textBox1.Text, textBox2.Text);

                if (visitanteBindingSource.Count > 0)
                    fotoPictureBox.Image = Classes.Utils.byteArrayToImage(((visitante)visitanteBindingSource.Current).foto);
            }
        }

        private void visitanteBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                carregaGrid();
            }
            catch
            {
            }
        }

        public void carregaGrid()
        {
            var query = from v in vaBL.selecionar(((visitante)visitanteBindingSource.Current).id_visitante)
                        where v.data_saida == null
                        select new { v.id_visitante_apartamento, v.apartamento.id_bloco, v.apartamento.numero, v.identificacao, v.data_entrada, v.data_saida };

            var results = query.ToList();

            dtGrdVwBloco.DataSource = results;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = dtGrdVwBloco.CurrentRow;
                int x = Convert.ToInt32(row.Cells["id_visitante_apartamento"].Value);

                visitante_apartamento va = vaBL.selecionarID(x);
                va.data_saida = DateTime.Now;
                vaBL.salvar(va, "edit");

                carregaGrid();

                MessageBox.Show("Executado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            FrmListVisitas v = new FrmListVisitas(((visitante)visitanteBindingSource.Current).id_visitante);
            v.ShowDialog();
        }
    }

}
